/////////////////////////////////////////////
//BookMarkTreeMgr
/////////////////////////////////////////////
function BookMarkTreeMgr ()
{
	this.m_bookMarkTreeData = null;
	this.m_callback = null;
	this.m_bookMarkUrl = null;
	this.m_domName = null;
	this.m_currentNodeID = null;
	this.m_preNodeID = null;
	var _self = this;
	
	this.loadBookMarkTreeData = function (docID, bookMarkUrl, callback)
	{
		_self.m_callback = callback;
		_self.m_bookMarkUrl = bookMarkUrl;
		var xhr = new XMLHttpRequest();
		xhr.onreadystatechange = function () {			
			if (xhr.readyState == xhr.DONE) {
				var status = "failed";
				if (xhr.response) {
						if(xhr.status == 200){
							readFileToArrayBuffer(xhr.response,callback,_self);
							status = "success";
						}					
					}
				if(status == "failed" && _self.m_callback != null)_self.m_callback(status);
			}
		};
		var url = _self.m_bookMarkUrl + "?fileid=" + docID;
		xhr.open("POST", url, true);
		xhr.responseType = "blob";
		xhr.send(); 
	};
	
	this.createBookMarkTree = function (buf)
	{
		_self.m_bookMarkTreeData = new PDBookMarkTreeData();
		var binaryReader = new BinaryReader(buf);
		_self.m_bookMarkTreeData.praseBookMarkTreeData(binaryReader);
		_self.showBookMark();
	};
	
	this.setShowDiv = function (domname)
	{
		_self.m_domName = domname;
	};
	
	this.showBookMark = function ()
	{
		bookMarkTree.add(0, -1, "", "");
	
		for (var i = 0; i < _self.m_bookMarkTreeData.getBookMarkCount(); ++ i)
		{
			var bookMarkData = _self.m_bookMarkTreeData.getBookMarkDataByIndex(i);
			var ID = bookMarkData.m_nID;
			var parentID = bookMarkData.m_nParentID;
			var title = bookMarkData.m_strTitle;
			bookMarkTree.add(ID, parentID, title, "");
		}
		document.getElementById(_self.m_domName).innerHTML = bookMarkTree.toString();
		$(".nodeText").click( function (){
				_self.m_preNodeID = _self.m_currentNodeID;
				_self.m_currentNodeID = $(this).attr("id");
				
				if (_self.m_preNodeID)
					document.getElementById("ibookMarkTree" + _self.m_preNodeID).src = "images/dtree/bk.png";
				
				document.getElementById("ibookMarkTree" + _self.m_currentNodeID).src = "images/dtree/bk1.png";
				
				var selectedBookMark = _self.m_bookMarkTreeData.getBookMarkDataByIndex(_self.m_currentNodeID - 1);
				if (selectedBookMark != null)
				{
					var dest = selectedBookMark.m_arrDest;
					var tmp = document.getElementById("MainFrame_ViewContainer_PageContainer_" + (dest.m_nPageIndex));
					if(tmp == null){
						var str = "<div style=\"position: relative; height: 1056px; width: 816px; margin-left: 155px;\" id=\"MainFrame_ViewContainer_PageContainer_" + (dest.m_nPageIndex) + "\" class=\"fwr_page\">";
						$(".jspPaneTree").append(str);
					}
					reader.goToPage(dest.m_nPageIndex);
				}
			}
		);		
	};
}
